<template>
	<uni-popup type="top" ref="pagePopupRef">
		<view  style="width:100vw">
			<uni-list>
				<uni-list-item title="波特率">
					<template v-slot:footer>
						<picker mode="selector" :range="baudRateRange" @change="(v)=>serialOptChange('baudRate',baudRateRange,v)">
							<view>{{gSettings.baudRate}}</view>
						</picker>
					</template>
				</uni-list-item>
				<uni-list-item title="数据位">
					<template v-slot:footer>
						<picker mode="selector" :range="dataBitsRange" @change="(v)=>serialOptChange('dataBits',dataBitsRange,v)">
							<view>{{gSettings.dataBits}}</view>
						</picker>
					</template>
				</uni-list-item>
				<uni-list-item title="停止位">
					<template v-slot:footer>
						<picker mode="selector" :range="stopBitsRange" @change="(v)=>serialOptChange('stopBits',stopBitsRange,v)">
							<view>{{gSettings.stopBits}}</view>
						</picker>
					</template>
				</uni-list-item>
				<uni-list-item title="校验位" >
					<template v-slot:footer>
						<picker mode="selector" :range="parityRange" @change="(v)=>serialOptChange('parity',parityRange,v)">
							<view>{{gSettings.parity}}</view>
						</picker>
					</template>
				</uni-list-item>
				<!-- <uni-list-item title="写入追加换行" >
					<template v-slot:footer>
						<picker mode="selector" :range="appendRange" @change="(v)=>serialOptChange('autoWriteAppend',appendRange,v)">
							<view>{{gSettings.autoWriteAppend}}</view>
						</picker>
					</template>
				</uni-list-item> -->
			</uni-list>
			<uni-popup ref="popup" type="dialog">
				<uni-popup-dialog mode="input" 
					title="请输入波特率"
					placeholder="请输入波特率"
					:duration="2000" 
					:before-close="true" 
					@close="popupClose" 
					@confirm="popupConfirm">
				</uni-popup-dialog>
			</uni-popup>
			<uni-popup ref="messagePopup" type="message">
			<uni-popup-message type="error" :message="popupMessage" :duration="1000"></uni-popup-message>
		</uni-popup>
		</view>
	</uni-popup>
</template>

<script setup>
	import {ref} from "vue"
	import { useGSettingsStore } from '@/store'
	const gSettings = useGSettingsStore()
	const baudRateRange=[2400,3600,4800,9600,14400,19200,28800,38400,57600,115200,230400,460800,921600,'自定义'];
	const dataBitsRange=[5,6,7,8];
	const stopBitsRange=[1,1.5,2];
	const parityRange=['None','Odd','Even',"Mark","Space"];
	const appendRange=['无','\\r\\n(0x0D0A)','\\n(0x0A)',"\\r(0x0D)"];
	
	const pagePopupRef = ref(null);
	const show = ()=>{
		pagePopupRef.value.open();
	};
	
	const close=()=>{
		pagePopupRef.value.close();
	};
	
	const serialOptChange=(item,range,v)=>{
		if(item == 'baudRate' && isNaN(range[v.detail.value])){
			popupOpen();
		}else{
			gSettings[item]=range[v.detail.value];
		}
	}
	
	const popup = ref(null);
	const popupOpen=()=>{
		popup.value.open();
	}
	
	const popupClose=()=>{
		popup.value.close();
	}
	
	const popupConfirm=(v)=>{
		console.log('popupConfirm',v);
		if(!isNaN(v)){
			gSettings.baudRate=v;
			popup.value.close();
		}else{
			openMessage('请输入数字')
		}		
	}
	
	const messagePopup=ref(null)
	const popupMessage=ref('')
	const openMessage=(message)=>{
		popupMessage.value=message;
		messagePopup.value.open();
	}
	
	defineExpose({ show })
</script>

<style>

</style>
